package esdemo;

import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.Before;
import org.junit.Test;

public class QueryDsl {
	private TransportClient client;

	@Before
	public void init() throws UnknownHostException {
		client = TransportClient.builder().build()
				.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
	}

	@SuppressWarnings("unused")
	@Test
	public void matchAll() {
		MatchAllQueryBuilder qb = matchAllQuery();
	}

	///////////// 全文索引 /////////////
	/**
	 * The standard query for performing full text queries, including fuzzy
	 * matching and phrase or proximity queries.
	 */
	@Test
	public void matchQuery() {
		MatchQueryBuilder qb = QueryBuilders.matchQuery("user", "kimchy2");

		SearchResponse response = client.prepareSearch("twitter").setQuery(qb).get();
		System.out.println(response);
	}

	@Test
	public void multiMatchQuery() {
		MultiMatchQueryBuilder qb = QueryBuilders.multiMatchQuery("kimchy2", // text
				"user", // fields
				"age" // fields
		);
		SearchResponse response = client.prepareSearch("twitter").setQuery(qb).get();
		System.out.println(response);
	}

	@SuppressWarnings("unused")
	@Test
	public void commonTermsQuery(){
		QueryBuilder qb = QueryBuilders.commonTermsQuery("name",    
                "kimchy"); 
	}
	
	@SuppressWarnings("unused")
	@Test
	public void queryStringQuery(){
		QueryBuilder qb = QueryBuilders.queryStringQuery("+kimchy -elasticsearch");
	}
	
	@SuppressWarnings("unused")
	@Test
	public void simpleQueryStringQuery(){
		QueryBuilder qb = QueryBuilders.simpleQueryStringQuery("+kimchy -elasticsearch"); 
	}
	
	
	
}
